package jus.aor.mobilagent.kernel;

import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;

public class Agent implements _Agent {

	private static final long serialVersionUID = 2732710730437497083L;
	private Route itineraire ;
	transient protected AgentServer serv;
	transient protected BAMLoader loader;
	transient protected String serverName;
	
	public void run(){
		try {
		itineraire.next().action.execute();
		}catch(NullPointerException e){
			System.out.println("le serveur "+serverName+" n'a pas le service demandé, passons à la prochaine étape");
		}
		if(itineraire.hasNext()){
				serv.send(itineraire.get().server,loader, this);
		}
	}

	public void init(AgentServer agentServer, String serverName) {
		this.serv = agentServer;
		this.serverName = serverName;
	}
	public void init(BAMLoader loader, AgentServer server, String serverName) {
		this.loader = loader;
		this.serv = server;
		this.serverName = serverName;
		//init_Route(serverName);
	}
	void init_Route(_Action a){
		try {
			URI uri = new URI("mobilagent://"+InetAddress.getLocalHost().getHostName()+":"+serv.port);
			 itineraire = new Route(new Etape(uri,a));
		} catch (URISyntaxException e) {
			e.printStackTrace();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}	
	}
	public void addEtape(Etape etape) {
		this.itineraire.add(etape);
	}

	@Override
	public void addLoader(BAMLoader l) {
		loader = l;
	}

}
